package com.shuaiche.yeguan;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.amap.api.location.AMapLocationClient;
import com.amap.api.services.core.ServiceSettings;
import com.shuaiche.yeguan.config.SCUserInfoConfig;
import com.shuaiche.yeguan.model.SCUserInfoResponse;
import com.shuaiche.yeguan.ui.login.LoginActivity;
import com.shuaiche.yeguan.ui.login.SCLoginWrapHelper;
import com.shuaiche.yeguan.util.SPUtils;
import com.shuaiche.yeguan.views.dialog.PrivacyDialog;

import androidx.appcompat.app.AppCompatActivity;

/**
 *
 */
public class LoadingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_loading);

        initViews();
    }

    private void initViews() {
        boolean isAgree = SPUtils.getBoolean(this,SPUtils.IS_AGREE,false);

        if (isAgree){
            //判断是否登录，未登录则登录，已登录前往主页
            SCUserInfoResponse userInfoResponse = SCUserInfoConfig.getUserinfo();
            if (userInfoResponse == null){
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SCLoginWrapHelper.starLogintActivity(LoadingActivity.this);
                        finish();
                    }
                },1500);

            }else {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        startActivity(new Intent(LoadingActivity.this,MainActivity.class));
                        finish();
                    }
                },1000);
            }
        }else {
            //隐私授权
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    showPrivacyDialog();
                }
            },1000);

        }
    }

    private void showPrivacyDialog() {
        PrivacyDialog dialog = new PrivacyDialog(this);
        dialog.setOnPrivacyResultListener(new PrivacyDialog.OnPrivacyResultListener() {
            @Override
            public void onConfirm() {
                // 用户同意隐私政策
                SPUtils.saveBoolean(LoadingActivity.this,SPUtils.IS_AGREE,true);

                AMapLocationClient.updatePrivacyShow(LoadingActivity.this,true,true);
                ServiceSettings.updatePrivacyShow(LoadingActivity.this,true,true);
                AMapLocationClient.updatePrivacyAgree(LoadingActivity.this,true);
                ServiceSettings.updatePrivacyAgree(LoadingActivity.this,true);
                SCApplication.scApplication.initSDKS();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        SCLoginWrapHelper.starLogintActivity(LoadingActivity.this);
                        finish();
                    }
                },1000);


            }

            @Override
            public void onRefuse() {

                finish();
            }
        });
        dialog.setCancelable(false);
        dialog.show();
    }

    private void show() {
//        // Show the system bar
//        if (Build.VERSION.SDK_INT >= 30) {
//            mContentView.getWindowInsetsController().show(
//                    WindowInsets.Type.statusBars() | WindowInsets.Type.navigationBars());
//        } else {
//            mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
//        }
    }
}